package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MarketCommentServiceImpl implements MarketCommentService {
    @Autowired
    MarketCommentMapper commentMapper;
    @Override
    public List<MarketComment> list(Integer page, Integer limit, String userId, String valueId, String sort, String order) {
        //mybatis从数据库中查询数据
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.setOrderByClause(sort + " " + order);
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        PageHelper.startPage(page, limit);
        return commentMapper.selectByExample(marketCommentExample);
    }

    @Override
    public void delete(MarketComment marketComment) {
        commentMapper.deleteByPrimaryKey(marketComment.getId());
    }
}

